﻿// Copyright (c) 2012 - 2020 Jared Zheng <jaredz at outlook dot com>
// Buddha Framework is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Create : 2012-12-01 version 0.1 Jared Zheng <jaredz at outlook dot com>
// Update : 2018-12-05 version 3.1.5 Jared Zheng <jaredz at outlook dot com>

#include "stdafx.h"
#include "ServerConsole.h"
#include "MasterHandler.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// CMasterHandler
CMasterHandler::CMasterHandler(void)
: m_uMasterTotal(0)
, m_uMasterOnline(0)
, m_uMasterLoad(0)
, m_uServiceCount(0)
, m_uServiceOnline(0)
, m_uServiceTotal(0)
, m_uGateCount(0)
, m_uGateOnline(0)
, m_uGateTotal(0)
{
}

CMasterHandler::~CMasterHandler(void)
{
}

void CMasterHandler::OnLive(bool bStart)
{
    CStringFix strTemp = GetName();
    if (bStart)
    {
        strTemp += TF("Start---");

        CStrAddr strAddr;

        GServerConsoleInst->m_Config.GetServerAddr(CServerConfig::CFG_LOAD_MASTER, CServerConfig::CFG_LOAD_NONE, strAddr);
        strTemp.AppendFormat(TF("TCP Int-Listen=[%s]:%d"), *strAddr.strIp, strAddr.usPort);
    }
    else
    {
        strTemp += TF("Stop");

        m_uMasterTotal   = 0;
        m_uMasterOnline  = 0;
        m_uMasterLoad    = 0;
        m_uServiceCount  = 0;
        m_uServiceOnline = 0;
        m_uServiceTotal  = 0;
        m_uGateCount     = 0;
        m_uGateOnline    = 0;
        m_uGateTotal     = 0;
    }
    LOG_INFO(GServerConsoleInst->m_FileLog, *strTemp);
}

void CMasterHandler::OnLink(Int nServerIndex, uintptr_t utData)
{
    CStringFix strTemp = GetName();
    if (nServerIndex == PAKT_CENTER)
    {
        strTemp += TF("TCP Int-Connect Center=");

        CNetAddr* pAddr = (reinterpret_cast<CNetAddr*>(utData));

        CStrAddr strAddr;

        GServerConsoleInst->m_Config.GetServerAddr(CServerConfig::CFG_LOAD_CENTER, CServerConfig::CFG_LOAD_NONE, strAddr);
        strTemp.AppendFormat(TF("[%s]:%d"), *strAddr.strIp, strAddr.usPort);

        GServerConsoleInst->m_NetworkPtr->TranslateAddr(strAddr.strIp, strAddr.usPort, *pAddr, false);
        strTemp.AppendFormat(TF("([%s]:%d)"), *strAddr.strIp, strAddr.usPort);
    }
    else if (nServerIndex == PAKT_MASTERDB)
    {
        strTemp += TF("TCP Int-Connect MasterDB=");

        if (utData != 0)
        {
            CNetAddr* pAddr = (reinterpret_cast<CNetAddr*>(utData));

            CStrAddr strAddr;

            GServerConsoleInst->m_Config.GetServerAddr(CServerConfig::CFG_LOAD_MASTERDB, 0, strAddr);
            strTemp.AppendFormat(TF("[%s]:%d"), *strAddr.strIp, strAddr.usPort);

            GServerConsoleInst->m_NetworkPtr->TranslateAddr(strAddr.strIp, strAddr.usPort, *pAddr, false);
            strTemp.AppendFormat(TF("([%s]:%d)"), *strAddr.strIp, strAddr.usPort);
        }
        else
        {
            strTemp += TF("---");
        }
    }
    else if (nServerIndex == PAKT_AUTHOR)
    {
        strTemp += TF("UDP Int-Listen Author=");

        CNetAddr* pAddr = (reinterpret_cast<CNetAddr*>(utData));

        CStrAddr strAddr;
        GServerConsoleInst->m_NetworkPtr->TranslateAddr(strAddr.strIp, strAddr.usPort, *pAddr, false);
        strTemp.AppendFormat(TF("[%s]:%d"), *strAddr.strIp, strAddr.usPort);
    }
    else
    {
        assert((nServerIndex == PAKT_SERVICE) || (nServerIndex == PAKT_GATE));
        AddServerData(nServerIndex, utData);
        return;
    }
    LOG_INFO(GServerConsoleInst->m_FileLog, *strTemp);
}

void CMasterHandler::OnUpdate(Int nServerIndex, uintptr_t utData)
{
    UpdateServerData(nServerIndex, utData);
}

void CMasterHandler::OnSync(Int nServerIndex, uintptr_t utData)
{
    if (nServerIndex == PAKT_SERVICE)
    {
        PSERVER_STATUS pStatus = reinterpret_cast<PSERVER_STATUS>(utData);
        m_uServiceTotal  = pStatus->uTotal;
        m_uServiceOnline = pStatus->uOnline;
        m_uServiceCount  = pStatus->usLoad;
    }
    else if (nServerIndex == PAKT_GATE)
    {
        PSERVER_STATUS pStatus = reinterpret_cast<PSERVER_STATUS>(utData);
        m_uGateTotal  = pStatus->uTotal;
        m_uGateOnline = pStatus->uOnline;
        m_uGateCount  = pStatus->usLoad;
    }
    else
    {
        assert(nServerIndex == PAKT_MASTER);
        PSERVER_STATUS pStatus = reinterpret_cast<PSERVER_STATUS>(utData);
        m_uMasterTotal  = pStatus->uTotal;
        m_uMasterOnline = pStatus->uOnline;
        m_uMasterLoad   = pStatus->usLoad / DATAD_TOPERCENT;
    }
}

void CMasterHandler::OnSync(Int, CStream&)
{
}

void CMasterHandler::OnUnlink(Int nServerIndex, uintptr_t utData)
{
    if (nServerIndex == PAKT_CENTER)
    {
        LOG_INFO(GServerConsoleInst->m_FileLog, TF("Master:TCP Int-Connect Center Closed"));
    }
    else if (nServerIndex == PAKT_MASTERDB)
    {
        LOG_INFO(GServerConsoleInst->m_FileLog, TF("Master:TCP Int-Connect MasterDB Closed"));
    }
    else if (nServerIndex == PAKT_AUTHOR)
    {
        LOG_INFO(GServerConsoleInst->m_FileLog, TF("Master:UDP Int-Listen Author Closed"));
    }
    else
    {
        assert((nServerIndex == PAKT_SERVICE) || (nServerIndex == PAKT_GATE));
        RemoveServerData(nServerIndex, utData);
    }
}

void CMasterHandler::UpdateServerInfo(void)
{
    CStringFix strTemp = GetName();
    strTemp.Format(TF("[Servers=%u/%u, Load=%d%%][Service:Count=%u, %u/%u][Gate:Count=%u, %u/%u]"),
                   m_uMasterOnline, m_uMasterTotal, m_uMasterLoad,
                   m_uServiceCount, m_uServiceOnline, m_uServiceTotal,
                   m_uGateCount, m_uGateOnline, m_uGateTotal);
    LOG_INFO(GServerConsoleInst->m_FileLog, *strTemp);
}

PCXStr CMasterHandler::GetName(void)
{
    return TF("Master:");
}






